package com.shengqi.xinnongren.work.ui.home.presenter;

import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.model.MyCenterBean;
import com.shengqi.xinnongren.work.ui.home.view.HomePage5View;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HomePage5Presenter extends BasePresenter<HomePage5View> {
    public void getMyCenterPage() {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), Constants.getJsonData(null, UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().myCenterPage(body), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                MyCenterBean bean = new Gson().fromJson(httpResult.data, MyCenterBean.class);
                getView().showMyCenterPage(bean);
            } else if (httpResult.result == -1) {
                UserManager.getInstance().clearLoginData();
                getView().onTokenInvalid();
            } else {
                getView().onRequestFailed(httpResult.message);
            }
        });
    }

}
